home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / graphics / display / mpega / src / bitstream.a < prev    next >
Encoding:
Text File  |  1997-11-20  |  10.9 KB  |  399 lines

  1. ;------------------------------------------------------------------------------
  2. ;
  3. ;   File    :    BitStream.a
  4. ;
  5. ;   Author  :    Stéphane TAVENARD
  6. ;
  7. ;   $VER:   BitStream.a  1.2  (29/07/1995)
  8. ;
  9. ;   (C) Copyright 1995-1995 Stéphane TAVENARD
  10. ;    All Rights Reserved
  11. ;
  12. ;   #Rev|   Date   |              Comment
  13. ;   ----|----------|--------------------------------------------------------
  14. ;   0    |12/04/1995| Initial revision                      ST
  15. ;   1    |03/06/1995| First release (no more C code)                       ST
  16. ;   2    |23/06/1995| Aminet release                      ST
  17. ;   3    |29/07/1995| Added a6 register save                  ST
  18. ;
  19. ;   ------------------------------------------------------------------------
  20. ;
  21. ;   BitStream files handling
  22. ;
  23. ;------------------------------------------------------------------------------
  24.  
  25.           IFND       INCLUDE_SOURCE
  26.           incdir   "include:"
  27.           include  "exec/funcdef.i"
  28.           include  "exec/exec.i"
  29.           include  "exec/exec_lib.i"
  30.           include  "dos/dos.i"
  31.           include  "dos/dos_lib.i"
  32.           XREF       _DOSBase
  33.           ENDC
  34.  
  35.           include  "bitstream.i"
  36.  
  37.           XDEF       @BSTR_open
  38.           XDEF       @BSTR_close
  39.           XDEF       @BSTR_rewind
  40.           XDEF       @BSTR_end
  41.           XDEF       @BSTR_seek_tell
  42.           XDEF       @BSTR_read_bit
  43.           XDEF       @BSTR_read_bits
  44.           XDEF       @BSTR_read_signed_bits
  45.           XDEF       @BSTR_skip_bits
  46.           XDEF       @BSTR_read3_bits
  47.           XDEF       @BSTR_read2_bits
  48.           XDEF       @BSTR_seek_sync
  49. ;
  50.           IFND       INCLUDE_SOURCE
  51. CALLEXEC      MACRO
  52.           move.l  (_SysBase).w,a6
  53.           jsr      _LVO\1(a6)
  54.           ENDM
  55. _SysBase      EQU      4
  56.  
  57. CALLDOS       MACRO
  58.           move.l  _DOSBase,a6
  59.           jsr      _LVO\1(a6)
  60.           ENDM
  61.           ENDC
  62.  
  63. ;
  64.           section  ASMCODE,code
  65.  
  66.  
  67. ;          Close a bitstream file
  68. ;
  69. ;          a0 = bitstream structure ptr
  70. ;
  71. @BSTR_close
  72.           movem.l  a2/a6/d2,-(sp)
  73.           move.l   a0,a2
  74.           tst.l    a2
  75.           beq       BSTR_close9
  76.           move.l   BSTR_file_ptr(a2),d1
  77.           beq       BSTR_close7
  78.           CALLDOS  Close
  79.           clr.l    BSTR_file_ptr(a2)
  80. BSTR_close7      move.l   BSTR_buffer(a2),a1
  81.           tst.l    a1
  82.           beq       BSTR_close8
  83.           move.l   BSTR_buffer_size(a2),d0
  84.           CALLEXEC FreeMem
  85.           clr.l    BSTR_buffer(a2)
  86. BSTR_close8      move.l   a2,a1
  87.           moveq.l  #BSTR_SIZEOF,d0
  88.           CALLEXEC FreeMem
  89. BSTR_close9      movem.l  (sp)+,a2/a6/d2
  90.           rts
  91.  
  92. ;          Open a bitstream file
  93. ;
  94. ;          a0 = name
  95. ;          d0.l = buffer size
  96. ;          d1.l = access mode
  97. ;          -> d0.l = bitstream structure ptr (=0 if error)
  98. @BSTR_open
  99.           movem.l  a2/a6/d2,-(sp)
  100.           move.l   a0,name
  101.           move.l   d0,buffer_size
  102.           move.l   d1,mode
  103.           clr.l    bstr_ptr
  104.           cmp.l    #BSTR_MODE_READ,d1
  105.           bne       BSTR_open8        ; not implemented yet
  106.  
  107.           moveq.l  #BSTR_SIZEOF,d0
  108.           move.l   #MEMF_ANY|MEMF_CLEAR,d1
  109.           CALLEXEC AllocMem
  110.           move.l   d0,bstr_ptr
  111.           beq       BSTR_open7        ; fail
  112.           move.l   d0,a2
  113.           move.l   buffer_size,d0
  114.           move.l   d0,BSTR_buffer_size(a2)
  115.           move.l   #MEMF_ANY,d1
  116.           CALLEXEC AllocMem
  117.           move.l   d0,BSTR_buffer(a2)
  118.           beq       BSTR_open7        ; fail
  119.           move.l   mode,BSTR_mode(a2)
  120.           move.l   name,d1
  121.           move.l   #MODE_OLDFILE,d2
  122.           CALLDOS  Open
  123.           move.l   d0,BSTR_file_ptr(a2)
  124.           beq       BSTR_open7        ; fail
  125.           move.l   a2,d0
  126.           bra       BSTR_open9        ; success
  127. BSTR_open7
  128.           move.l   bstr_ptr,a0
  129.           bsr       @BSTR_close
  130. BSTR_open8      clr.l d0
  131. BSTR_open9      movem.l  (sp)+,a2/a6/d2
  132.           rts
  133.  
  134. ;          Seek to start of the bitstream
  135. ;
  136. ;          a0 = bitstream structure ptr
  137. ;          -> d0.l = 0 if fail
  138. ;
  139. @BSTR_rewind      movem.l  a2/a6/d2-d3,-(sp)
  140.           tst.l    a0
  141.           move.l   a0,a2
  142.           beq       BSTR_rewind9
  143.           move.l   BSTR_file_ptr(a2),d1
  144.           moveq.l  #0,d2
  145.           moveq.l  #OFFSET_BEGINNING,d3
  146.           CALLDOS  Seek
  147.           tst.l    d0
  148.           bmi       BSTR_rewind8
  149.           clr.l    BSTR_current_bit_index(a2)
  150.           clr.l    BSTR_remaining_bits(a2)
  151.           clr.l    BSTR_bitstream_size(a2)
  152.           clr.l    BSTR_end_of_stream(a2)
  153.           moveq.l  #1,d0
  154.           bra       BSTR_rewind9
  155. BSTR_rewind8      clr.l    d0
  156. BSTR_rewind9      movem.l  (sp)+,a2/a6/d2-d3
  157.           rts
  158.  
  159. ;          Test if end of bistream is reached
  160. ;
  161. ;          a0 = bitstream structure ptr
  162. ;          -> d0.l = 0 if not end of bitstream
  163. @BSTR_end
  164.           move.l   BSTR_end_of_stream(a0),d0
  165.           rts
  166.  
  167. ;          Get the current seek bit of bistream
  168. ;          a0 = bitstream structure ptr
  169. ;          -> d0.l = seek value
  170. @BSTR_seek_tell
  171.           move.l   BSTR_bitstream_size(a0),d0
  172.           rts
  173.  
  174.  
  175. ;          Fill in the read buffer of a bitstream
  176. ;
  177. ;          a0 = bitstream
  178. BSTR_fill_buffer
  179.           movem.l  d0-d7/a0-a6,-(sp)
  180.           tst.l    BSTR_end_of_stream(a0)
  181.           bne       BSTR_fill_buffer9        ; end of stream !
  182.           move.l   BSTR_buffer(a0),a1
  183.           move.l   BSTR_buffer_size(a0),d3
  184.           move.l   BSTR_current_bit_index(a0),d0
  185.           asr.l    #3,d0            ; current byte index
  186.           lea.l    (a1,d0.l),a2             ; last unread bytes
  187.           and.l    #7,BSTR_current_bit_index(a0) ; reset current bit index
  188.           move.l   BSTR_remaining_bits(a0),d0
  189. BSTR_fill_buffer1 tst.l    d0
  190.           ble       BSTR_fill_buffer2        ; no remaining bits
  191.           move.b   (a2)+,(a1)+              ; copy remaining bytes
  192.           subq.l   #8,d0
  193.           subq.l   #1,d3            ; bytes to read --
  194.           bra       BSTR_fill_buffer1
  195. BSTR_fill_buffer2 move.l   a0,-(sp)
  196.           move.l   BSTR_file_ptr(a0),d1
  197.           move.l   a1,d2           ; read buffer
  198.           CALLDOS  Read
  199.           move.l   (sp)+,a0
  200.           asl.l    #3,d0           ; read bytes * 8 -> read bits
  201.           ble       BSTR_fill_buffer8       ; read bits <= 0 -> end of stream
  202.           add.l    d0,BSTR_remaining_bits(a0)
  203.           bra       BSTR_fill_buffer9
  204. BSTR_fill_buffer8 move.l   #-1,BSTR_end_of_stream(a0)   ; end of stream !
  205. BSTR_fill_buffer9 movem.l  (sp)+,d0-d7/a0-a6
  206.           rts
  207.  
  208.  
  209. ;          Read 1 bit (unsigned) from a bitstream
  210. ;
  211. ;          a0 = bitstream
  212. ;          -> d0 = bit value
  213. ;          a0 is preserved
  214. ;          a1 & d1 are affected
  215. ;
  216. @BSTR_read_bit
  217.           moveq.l  #1,d1
  218. ;           bra        @BSTR_read_bits
  219.  
  220. ;          Read some bits (unsigned) from a bitstream
  221. ;
  222. ;          a0 = bitstream
  223. ;          d1 = bit count (max=32)
  224. ;          -> d0 = bits value (=0 if bitstream is empty)
  225. ;          a0 & d1 are preserved
  226. ;          a1 is affected
  227. ;
  228. @BSTR_read_bits
  229. BSTR_read_bits1   sub.l    d1,BSTR_remaining_bits(a0)
  230.           bmi.s    BSTR_read_bits2          ; no more bits available
  231.           move.l   BSTR_current_bit_index(a0),d0
  232.           move.l   BSTR_buffer(a0),a1
  233.           bfextu   (a1){d0:d1},d0             ; read bits -> d0
  234.           add.l    d1,BSTR_current_bit_index(a0)
  235.           add.l    d1,BSTR_bitstream_size(a0)
  236.           rts
  237. BSTR_read_bits2   add.l    d1,BSTR_remaining_bits(a0)      ; restore old count
  238.           bsr       BSTR_fill_buffer
  239.           tst.l    BSTR_end_of_stream(a0)
  240.           beq       BSTR_read_bits1          ; not end of bitstream
  241. BSTR_read_bits9   moveq.l  #0,d0
  242.           rts
  243.  
  244. ;          Read some bits (signed) from a bitstream
  245. ;
  246. ;          a0 = bitstream
  247. ;          d1 = bit count (max=32)
  248. ;          -> d0 = bits value (=0 if bitstream is empty)
  249. ;          a0 & d1 are preserved
  250. ;          a1 is affected
  251. ;
  252. @BSTR_read_signed_bits
  253. BSTR_read_sbits1  sub.l    d1,BSTR_remaining_bits(a0)
  254.           bmi       BSTR_read_sbits2          ; no more bits available
  255.           move.l   BSTR_current_bit_index(a0),d0
  256.           move.l   BSTR_buffer(a0),a1
  257.           bfexts   (a1){d0:d1},d0             ; read signed bits -> d0
  258.           add.l    d1,BSTR_current_bit_index(a0)
  259.           add.l    d1,BSTR_bitstream_size(a0)
  260.           rts
  261. BSTR_read_sbits2  add.l    d1,BSTR_remaining_bits(a0)      ; restore old count
  262.           bsr       BSTR_fill_buffer
  263.           tst.l    BSTR_end_of_stream(a0)
  264.           beq       BSTR_read_sbits1          ; not end of bitstream
  265. BSTR_read_sbits9  moveq.l  #0,d0
  266.           rts
  267.  
  268. ;          Skip some bits from a bitstream
  269. ;
  270. ;          a0 = bitstream
  271. ;          d1 = length (can be > 32)
  272. ;          -> d0 = bit skipped count
  273. ;          a0, a1 & d1 are preserved
  274. ;
  275. @BSTR_skip_bits
  276. BSTR_skip_bits1   sub.l    d1,BSTR_remaining_bits(a0)
  277.           bmi.s    BSTR_skip_bits2         ; no more bits available
  278.           add.l    d1,BSTR_current_bit_index(a0)
  279.           add.l    d1,BSTR_bitstream_size(a0)
  280.           move.l   d1,d0
  281.           rts
  282. BSTR_skip_bits2   add.l    d1,BSTR_remaining_bits(a0)    ; restore old count
  283.           bsr       BSTR_fill_buffer
  284.           tst.l    BSTR_end_of_stream(a0)
  285.           beq       BSTR_skip_bits1         ; not end of bitstream
  286.           clr.l    d0
  287.           rts
  288.  
  289. ;          Read three bits-packets (unsigned) from a bitstream
  290. ;
  291. ;          a0 = bitstream
  292. ;          d1 = bit count for each packet (max=32)
  293. ;          -> d4, d5, d6 = bits values packets
  294. ;          a0 & d1 are preserved
  295. ;          a1 & d2 are modified !!!
  296. ;
  297. @BSTR_read3_bits
  298. BSTR_read3_bits1  move.l   d1,d2
  299.           add.l    d2,d2
  300.           add.l    d1,d2              ; d2 = 3*d1
  301.           sub.l    d2,BSTR_remaining_bits(a0)
  302.           bmi.s    BSTR_read3_bits2          ; no more bits available
  303.           move.l   BSTR_current_bit_index(a0),d0
  304.           move.l   BSTR_buffer(a0),a1
  305.           bfextu   (a1){d0:d1},d4             ; read bits -> d4
  306.           add.l    d1,d0              ; next packet
  307.           bfextu   (a1){d0:d1},d5             ; read bits -> d5
  308.           add.l    d1,d0              ; next packet
  309.           bfextu   (a1){d0:d1},d6             ; read bits -> d6
  310.           add.l    d2,BSTR_current_bit_index(a0)
  311.           add.l    d2,BSTR_bitstream_size(a0)
  312.           rts
  313. BSTR_read3_bits2  add.l    d2,BSTR_remaining_bits(a0)      ; restore old count
  314.           bsr       BSTR_fill_buffer
  315.           tst.l    BSTR_end_of_stream(a0)
  316.           beq       BSTR_read3_bits1          ; not end of bitstream
  317.           moveq.l  #0,d4
  318.           move.l   d4,d5
  319.           move.l   d4,d6
  320.           rts
  321.  
  322. ;          Read two bits-packets (unsigned) from a bitstream
  323. ;
  324. ;          a0 = bitstream
  325. ;          d1 = bit count for each packet (max=32)
  326. ;          -> d4, d5 = bits values packets
  327. ;          a0 & d1 are preserved
  328. ;          a1 & d2 are modified !!!
  329. ;
  330. @BSTR_read2_bits
  331. BSTR_read2_bits1  move.l   d1,d2
  332.           add.l    d2,d2              ; d2 = 2*d1
  333.           sub.l    d2,BSTR_remaining_bits(a0)
  334.           bmi.s    BSTR_read2_bits2          ; no more bits available
  335.           move.l   BSTR_current_bit_index(a0),d0
  336.           move.l   BSTR_buffer(a0),a1
  337.           bfextu   (a1){d0:d1},d4             ; read bits -> d4
  338.           add.l    d1,d0              ; next packet
  339.           bfextu   (a1){d0:d1},d5             ; read bits -> d5
  340.           add.l    d2,BSTR_current_bit_index(a0)
  341.           add.l    d2,BSTR_bitstream_size(a0)
  342.           rts
  343. BSTR_read2_bits2  add.l    d2,BSTR_remaining_bits(a0)      ; restore old count
  344.           bsr       BSTR_fill_buffer
  345.           tst.l    BSTR_end_of_stream(a0)
  346.           beq       BSTR_read2_bits1          ; not end of bitstream
  347.           moveq.l  #0,d4
  348.           move.l   d4,d5
  349.           rts
  350.  
  351. ;          Seek to the next sync. 8-bit aligned pattern
  352. ;
  353. ;          a0 = bitstream structure ptr
  354. ;          d0.l = sync. value
  355. ;          d1.l = sync. length
  356. ;          -> d0.l = 0 if not found
  357. ;
  358. @BSTR_seek_sync
  359.           movem.l  a2/d2-d4,-(sp)
  360.           move.l   d0,d2
  361.           move.l   d1,d3
  362.           clr.l    d4
  363.           bset       d1,d4
  364.           subq.l   #1,d4           ; d4 = bits mask
  365.           move.l   BSTR_bitstream_size(a0),d0
  366.           and.l    #7,d0
  367.           beq       BSTR_seek_sync3       ; already 8-bit aligned
  368.           moveq.l  #8,d1
  369.           sub.l    d0,d1           ; d1 = bits to skip
  370.           bsr       @BSTR_skip_bits
  371. BSTR_seek_sync3   move.l   d3,d1
  372.           bsr       @BSTR_read_bits       ; read first packet
  373.           move.l   d0,d3
  374.           moveq.l  #8,d1
  375. BSTR_seek_sync5   cmp.l    d3,d2
  376.           beq       BSTR_seek_sync8       ; ok sync. found
  377.           tst.l    BSTR_end_of_stream(a0)
  378.           bne       BSTR_seek_sync7       ; end of bitstream reached
  379.           lsl.l    d1,d3
  380.           and.l    d4,d3
  381.           bsr       @BSTR_read_bits       ; read next 8 bits
  382.           or.l       d0,d3           ; update bit packet
  383.           bra       BSTR_seek_sync5
  384. BSTR_seek_sync7   clr.l    d0
  385.           bra       BSTR_seek_sync9
  386. BSTR_seek_sync8   moveq.l  #-1,d0
  387. BSTR_seek_sync9   movem.l  (sp)+,a2/d2-d4
  388.           rts
  389.  
  390.           section  ASMVARS,bss
  391.  
  392.           even
  393.  
  394. name          ds.l       1
  395. buffer_size      ds.l       1
  396. mode          ds.l       1
  397. bstr_ptr      ds.l       1
  398.  
  399.